package com.aizuda.boot.modules.business.permission.service.impl;

import com.aizuda.boot.modules.business.permission.domain.entity.RoleDataIsolationConditionEntity;
import com.aizuda.boot.modules.business.permission.domain.request.RoleDataIsolationConditionAddDTO;
import com.aizuda.boot.modules.business.permission.mapper.RoleDataIsolationConditionMapper;
import com.aizuda.boot.modules.business.permission.service.RoleDataIsolationConditionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author sangyirong
 * @ClassName RoleDataIsolationConditionServiceImpl
 * @Version 1.0
 * @Date 2025/5/19 14:59
 */
@Service
public class RoleDataIsolationConditionServiceImpl extends ServiceImpl<RoleDataIsolationConditionMapper, RoleDataIsolationConditionEntity> implements RoleDataIsolationConditionService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCustom(Long id, List<RoleDataIsolationConditionAddDTO> customConditionList) {
        if (CollectionUtils.isEmpty(customConditionList)){
            return;
        }
        List<RoleDataIsolationConditionEntity> list = customConditionList.stream().map(item -> {
            RoleDataIsolationConditionEntity roleDataIsolationConditionEntity = new RoleDataIsolationConditionEntity();
            roleDataIsolationConditionEntity.setPermissionId(id);
            BeanUtils.copyProperties(item, roleDataIsolationConditionEntity);
            return roleDataIsolationConditionEntity;
        }).collect(Collectors.toList());
        saveBatch(list);
    }
}
